package com.nf.health.app.myfamily.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.ViewUtil;

/** 
 * @ClassName: OldSettongPhoneActivity 
 * @Description: 老人机设置联系方式
 * @author Ian 
 * @date 2015-11-18 下午3:19:47 
 *  
 */
public class OldSettongPhoneActivity extends BaseActivity {

	private EditText et_phone_num_new,et_code_num;

	private TextView et_iemi_num,et_phone_num;

	Button btn_get_code,btn_binding;

	String et_ipone_num1,imei,phone;

	String et_verification_code1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_old_settong_phone);
		Intent intent = getIntent();
		imei = intent.getStringExtra("imei");
		phone = intent.getStringExtra("phone");

		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub
		et_iemi_num = (TextView) findViewById(R.id.et_iemi_num);//imei
		et_iemi_num.setText(imei);//赋值

		et_phone_num = (TextView) findViewById(R.id.et_phone_num);//原手机号
		et_phone_num.setText(phone);//原手机号赋值

		et_phone_num_new = (EditText) findViewById(R.id.et_verification_code);//新手机号
		et_code_num = (EditText) findViewById(R.id.et_name_num);//验证码
		btn_get_code = ViewUtil.getView(this, R.id.btn_get_code);
		btn_binding = ViewUtil.getView(this, R.id.btn_binding);
		ViewUtil.setText(this, R.id.titlebar_title_tv, "设置联系方式");

		btn_get_code.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				et_ipone_num1 = et_phone_num_new.getText().toString();
				if (TextUtils.isEmpty(et_ipone_num1)) {
					showToast("手机号码不能为空!");
				}else{
					if (et_ipone_num1.length() != 11) {
						showToast("手机号码输入错误！");
					}else{
						provider.requestMSGCode(phone,"sms_ohter","verifycode");
					}

				}
			}
		});

		btn_binding.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				et_ipone_num1 = et_phone_num_new.getText().toString();
				if (TextUtils.isEmpty(et_ipone_num1)) {
					showToast("手机号码不能为空!");
				}else{
					if (et_phone_num_new.length() != 11) {
						showToast("手机号码输入错误！");
					}else{
//						et_verification_code1 = et_code_num.getText().toString();
//						if(null !=  Constants.LOCAL_CODE && !Constants.LOCAL_CODE.equals(et_verification_code1)){
//							showToast("验证码输入错误！");
//						}else{
							//绑定操作
						provider.requestMSGCodeCode(et_ipone_num1, et_code_num.getText().toString(), "sms_ohter", "yanzheng");

//						}
					}
				} 
			}
		});	
	}
	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("updatephone")) {
			String aa = object+"";
			if(aa.contains("成功")){
				showToast("手机号修改成功");
				finish();
			}
		}else if (actionName.equals("verifycode")) {
			hideLoading();
			showToast("验证码发送成功！");
		}else if ("yanzheng".equals(actionName)) {//判断验证码与手机号是否相符

			String msg = object+"";
			if(msg.contains("验证成功")){
				provider.requestUpdatePhone(getIntent().getStringExtra("touserid"),et_ipone_num1,"updatephone");
			}else{
				showToast("验证码错误");
			}
		}
	}
}
